package cmw.com.beijingnew.menudetailpager;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.apkfuns.logutils.LogUtils;
import com.viewpagerindicator.TabPageIndicator;

import org.xutils.common.util.LogUtil;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

import cmw.com.base.MenuDetailBasePager;
import cmw.com.beijingnew.R;
import cmw.com.beijingnew.activity.MainActivity;
import cmw.com.beijingnew.menudetailpager.tabdetailpager.TabDetailPager;

/**
 * 新闻详情页面
 * Created by Administrator on 2017/4/23.
 */
public class NewsMenuDetailPager extends MenuDetailBasePager {
    @ViewInject(R.id.tabPagerIndicator)
    private TabPageIndicator tabPagerIndicator;

    @ViewInject(R.id.viewpager)
    private ViewPager viewPager;

    @ViewInject(R.id.ib_tab_next)
    private ImageButton ib_tab_next;

    private JSONArray jsonArr;

    //TabDetailPager 列表
    private ArrayList<TabDetailPager> tabList;

    public NewsMenuDetailPager(Context context, JSONArray jsonArr) {
        super(context);
        this.jsonArr = jsonArr;
    }

    @Override
    protected View initView() {
        View view = View.inflate(context, R.layout.newsmenu_detail_pager, null);
        x.view().inject(NewsMenuDetailPager.this, view);
        initEvents();
        return view;
    }

    /**
     * 初始化事件
     */
    private void initEvents(){
        //Tab Title 右边方向箭头点击滑动事件
        ib_tab_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int nextTabIndex = viewPager.getCurrentItem()+1;
                viewPager.setCurrentItem(nextTabIndex);
            }
        });
    }

    @Override
    public void initData() {
        super.initData();
        LogUtil.i("新闻详情页面内容数据初始化了...");
        if(null == jsonArr || jsonArr.size() == 0) return;
        tabList = new ArrayList<>();
        for(int i=0, count = jsonArr.size(); i < count; i++){
            String tabTitle = jsonArr.getString(i);
            TabDetailPager tabPager = new TabDetailPager(context, tabTitle);
            tabList.add(tabPager);
        }
        viewPager.setAdapter(new MyViewPagerAdapter());
        tabPagerIndicator.setViewPager(viewPager);

        //注意以后的监听变化要改为 TabPagerIndicator 监听页面变化
        tabPagerIndicator.setOnPageChangeListener(new MyPageChangeListener());
    }

    /**
     *  TabPagerIndicator 设置页面滑动事件
     */
    private class MyPageChangeListener implements ViewPager.OnPageChangeListener{

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            MainActivity mainActivity = (MainActivity)context;
            if(position == 0){//可以全屏滑动
                mainActivity.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
            }else{//不可以全屏滑动
                mainActivity.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    }

    /**
     * ViewPager 自宝义适配器类
     */
    private class MyViewPagerAdapter extends PagerAdapter {

        @Override
        public CharSequence getPageTitle(int position) {
            String tabTitle = jsonArr.getString(position);
            return tabTitle;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            TabDetailPager tabDetailPager = tabList.get(position);
            View rootView = tabDetailPager.rootView;
            tabDetailPager.initData();
            container.addView(rootView);
            return rootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }

        @Override
        public int getCount() {
            return tabList.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }
}
